<?PHP
require_once WACT_ROOT.'controller/controller.inc.php';
require_once WACT_ROOT. 'util/dataspace.inc.php';
require_once APP_ROOT.'view/pub/layoutright.view.php';

require_once APP_ROOT.'model/common/user.php';
require_once APP_ROOT.'model/common/categories.php';
require_once APP_ROOT.'model/common/session.php';
require_once APP_ROOT.'model/common/article.php';
require_once APP_ROOT.'model/common/scrolling.php';
require_once APP_ROOT . "model/common/site.php";
require_once APP_ROOT . "model/common/video.php";


class ArticlesCategoryView extends LayoutRightView {
	function ArticlesCategoryView ($TemplateFile) {
		parent::LayoutRightView($TemplateFile);
	}
	function prepare(&$controller, &$request, &$responseModel){
		parent::prepare();
		if(!Session::isLogin())
			$this->Template->set('login',' ');

		if(APP_NAME!=""){
			$this->Template->set('app_name',APP_NAME);
		} else $this->Template->set('app_name','.');

		$this->Template->set('SITE_CSS',Site::getSiteTemplateCSS());
		$this->Template->set('SITE_BASE',Site::getSiteTemplateBase());
		$this->Template->set('SITE_TITLE',Site::getTitle());

         // AD BOXES
        $banner=&LogoBannersModel::getBanner('ROW1ADBOX1');
        $row1 = isset($banner[1]);
        $this->Template->set($banner[0], $banner[1]);
        $this->Template->set('ADBOX11banner_link', $banner[2]);
        $this->Template->set('ADBOX11banner_alt', $banner[3]);

        $banner=&LogoBannersModel::getBanner('ROW1ADBOX2');
        $row1 = $row1 || isset($banner[1]);
        $this->Template->set($banner[0], $banner[1]);
        $this->Template->set('ADBOX12banner_link', $banner[2]);
        $this->Template->set('ADBOX12banner_alt', $banner[3]);

        $banner=&LogoBannersModel::getBanner('ROW1ADBOX3');
        $row1 = $row1 || isset($banner[1]);
        $this->Template->set($banner[0], $banner[1]);
        $this->Template->set('ADBOX13banner_link', $banner[2]);
        $this->Template->set('ADBOX13banner_alt', $banner[3]);
        if ($row1) $this->Template->set('ROW1', 1);


        $banner=&LogoBannersModel::getBanner('ROW2ADBOX1');
        $row2 = isset($banner[1]);
        $this->Template->set($banner[0], $banner[1]);
        $this->Template->set('ADBOX21banner_link', $banner[2]);
        $this->Template->set('ADBOX21banner_alt', $banner[3]);

        $banner=&LogoBannersModel::getBanner('ROW2ADBOX2');
        $row2 = $row2 || isset($banner[1]);
        $this->Template->set($banner[0], $banner[1]);
        $this->Template->set('ADBOX22banner_link', $banner[2]);
        $this->Template->set('ADBOX22banner_alt', $banner[3]);

        $banner=&LogoBannersModel::getBanner('ROW2ADBOX3');
        $row2 = $row2 || isset($banner[1]);
        $this->Template->set($banner[0], $banner[1]);
        $this->Template->set('ADBOX23banner_link', $banner[2]);
        $this->Template->set('ADBOX23banner_alt', $banner[3]);
        if ($row2) $this->Template->set('ROW2', 1);

        $banner=&LogoBannersModel::getBanner('ROW3ADBOX1');
        $row3 = isset($banner[1]);
        $this->Template->set($banner[0], $banner[1]);
        $this->Template->set('ADBOX31banner_link', $banner[2]);
        $this->Template->set('ADBOX31banner_alt', $banner[3]);

        $banner=&LogoBannersModel::getBanner('ROW3ADBOX2');
        $row3 = $row3 || isset($banner[1]);
        $this->Template->set($banner[0], $banner[1]);
        $this->Template->set('ADBOX322banner_link', $banner[2]);
        $this->Template->set('ADBOX32banner_alt', $banner[3]);

        $banner=&LogoBannersModel::getBanner('ROW3ADBOX3');
        $row3 = $row3 || isset($banner[1]);
        $this->Template->set($banner[0], $banner[1]);
        $this->Template->set('ADBOX33banner_link', $banner[2]);
        $this->Template->set('ADBOX33banner_alt', $banner[3]);
        if ($row3) $this->Template->set('ROW3', 1);



		$sport_id=$request->getParameter('sport');
		$community_id=$request->getParameter('community');
		if ($sport_id!=null ) $sport=&CategoriesModel::getRecord($sport_id);
		if ($community_id!=null) $community=&CategoriesModel::getRecord($community_id);

		$lid=null;
		if($sport_id!=null) {
			$title=$sport->get('name');
			$this->Template->set('sport',$sport->get('name'));
			$this->Template->set('sport_id',$sport_id);
			$id=$sport_id;
			if($community_id!=null) {
				$title.=" - ".$community->get('name');
				$this->Template->set('all',' ');
				$this->Template->set('community',$community->get('name'));
				$this->Template->set('community_id',$community_id);
				$lid=$community_id;
			}
		}else {
			$title=$community->get('name');
			$id=$community_id;
			$this->Template->set('community',$community->get('name'));
			$this->Template->set('community_id',$community_id);
			$this->Template->set('onlycommunity','true');
		}

		$videoShow = Parameters::getVideoStatus();
		if ( $videoShow ) {
			// set video playlist XML file
        	$filename =& Video::getPlaylistFilename($sport_id,$community_id);
        	$this->Template->set('playlist',$filename);
        	Video::savePlaylistFile($sport_id,$community_id);

        	// prevent local caching
        	$rnumber = rand(1, 9999999);
        	$this->Template->set('rnumber',$rnumber);
        }


	    $this->Template->set('title',Site::getTitle().' - '.$title);

		$list=&CategoriesModel::getLinkedCategoriesList($id,$lid);
		$this->Template->setChildDataSource('linkedcategories',$list);
		$this->Template->set('highid',$id);

		$news =& Scrolling::getRendomRecord($sport_id,$community_id);
		if($news) {
            $headline = $news['headline'];
			$headline = Article::getShortText2($headline,32);
			$this->Template->set('headline',$headline);

			$this->Template->set('image_url',$news['image_url']);
			$this->Template->set('summary',$news['summary']);
			if(isset($news['orientation'])&&$news['orientation']==1)
				$this->Template->set('horizontal',' ');
		} else {
			$article=&Article::getRecordForScrolling($sport_id,$community_id);
			if($article) {
				$picture_url = $article['front_picture'];
                $headline = $article['headline'];
				$headline = Article::getShortText2($headline,32);
				$this->Template->set('headline',$headline);
				$this->Template->set('image_url',$picture_url);
				$this->Template->set('summary',$article['summary']);
				$this->Template->set('article',' ');
				$this->Template->set('art_id',$article['article_id']);
				$mypos = strpos($picture_url, '/'); // if starts with /, append .
				if ( $mypos === 0) $picture_url = '.'.$picture_url;
				//print  $picture_url;
				//$picture_url = str_replace(' ','%20',$picture_url); //REPLACE THE SPACES
				$image_prop = @getimagesize($picture_url);
				$width=$image_prop[0];
				$height=$image_prop[1];
                //if ( $width>300)  $width = 300;
                $width = 300;
				$this->Template->set('width',$width);
				//if($width>$height) $orientation='h';
				//else if($width<$height)$orientation='v';
				//else if($width==$height&&$width<=220) $orientation='v';
				//else $orientation='h';
				//if($orientation=='h')  {
					//$this->Template->set('horizontal',' ');
					//if($width>430) $this->Template->set('width','width=430');
				//}else if($width>240) $this->Template->set('width','width=240');

			}
		}

		$list_art=Article::getLatestArticles($sport_id,$community_id);
		$this->Template->setChildDataSource('latestheadlines',$list_art);

		$list_art2=Article::getLatestArticles2($sport_id,$community_id);
		$this->Template->setChildDataSource('latestarticles',$list_art2);






	}
}

class ArticlesCategoryPage extends PageController {
	function ArticlesCategoryPage() {
		parent::PageController();

        $site_id = Site::getTemplateSiteID();
        $mytemplate = 'pub/articles/category.html';


		$this->setDefaultView(new ArticlesCategoryView($mytemplate));
	}

}
?>
